﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sitecore.Models;

namespace Sitecore.Controllers
{
    public class AdminController : Controller
    {
        private int ItemsPerPage = 10;

        [Authorize(Roles = "Administrators")]
        public ActionResult Index(bool SortByBookTitle = true, int pageNumber = 0)
        {
            List<Book> list = new List<Book>();
            try
            {
                ViewData["pageNumber"] = pageNumber;
                ViewData["pageSize"] = ItemsPerPage;
                ViewData["itemsCount"] = DataProject.DBController.BookCount();

                var bookList = DataProject.DBController.GetListOfBook(ItemsPerPage, pageNumber, SortByBookTitle);
                foreach (DataProject.Book domainBook in bookList)
                {
                    list.Add(Book.FromDbModel(domainBook));
                }
            }
            catch { }
            return View(list);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Details(int id)
        {
            return View(Book.FromDbModel(DataProject.DBController.GetBookById(id)));
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Create(int authorsCount = 1)
        {
            Book book = new Book();
            ViewData["authorsCount"] = authorsCount;
            book._Authors = new List<Author>();
            for (int i = 0; i < authorsCount; i++)
            {
                book._Authors.Add(new Author { FirstName = String.Empty, LastName = String.Empty });
            }
            book.Year = DateTime.Now.Year;
            return View(book);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Book book, int authorsCount = 1)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    DataProject.Book domainBook = new DataProject.Book();
                    book.AssignTo(domainBook);
                    DataProject.DBController.CreateBook(domainBook);
                    return RedirectToAction("Index");
                }
                ViewData["authorsCount"] = authorsCount;
            }
            catch(Exception e)
            {
                ModelState.AddModelError("Exception while saving", e.Message);
            }
            return View(book);
        }

        [Authorize(Roles = "Administrators")]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, int authorsCount = 1)
        {
            Book book = Book.FromDbModel(DataProject.DBController.GetBookById(id));
            ViewData["authorsCount"] = authorsCount;
            while (authorsCount > book._Authors.Count)
            {
                book._Authors.Add(new Author());
            }
            return View(book);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, Book book)
        {
            try
            {
                DataProject.Book domainBook = new DataProject.Book();
                book.AssignTo(domainBook);
                DataProject.DBController.UpdateBook(domainBook);
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                ModelState.AddModelError("Exception while editing", e.Message);
            }
            ViewData["authorsCount"] = book._Authors.Count;
            return View(book);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            return View(Book.FromDbModel(DataProject.DBController.GetBookById(id)));
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id, Book book)
        {
           try
                {
                    DataProject.DBController.DeleteBook(id);
                    return RedirectToAction("Index");
                }
           catch (Exception e)
           {
               ModelState.AddModelError("Exception while deleting", e.Message);
           }
           return View(book);
        }
    }
}
